<?php
namespace Home\Controller;
use Think\Controller;
class NewsController extends CommonController {
	public function index()
	{
		unset($_SESSION['access_token']);

		$appId = APPID;
		$timestamp = time();
		$nonceStr = $this->getnonceStr();
		$jsapi_ticket = $this->getTicket();
		var_dump($jsapi_ticket);
		$url = "http://1o533n8683.imwork.net/P201606/weixin/api.php/home/news";

		$str = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$nonceStr."&timestamp=".$timestamp."&url=".$url;

		$signature = sha1($str);

		$this->assign('appId',$appId);
		$this->assign('timestamp',$timestamp);
		$this->assign('nonceStr',$nonceStr);
		$this->assign('signature',$signature);
		$this->display();
	}

	public function getnonceStr($num=16)
	{
		$str = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";
		$tempstr = "";
		for ($i=0; $i < $num ; $i++) { 
			$tempstr .= $str[mt_rand(0,61)];
		}
		return $tempstr;
	}

	public function getTicket()
	{
		$obj = new ApiController();
		$access_token = $obj->getAccessToken();
		var_dump($access_token);
		$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";
		$res = $this->wget($url);

		var_dump($res);

		return $res['ticket'];
	}
}